package cn.shenlong.portal.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Site;
import cn.shenlong.portal.mapper.SiteMapper;
import cn.shenlong.portal.service.SiteService;

@Service("siteService")
public class SiteServiceImpl implements SiteService {

	@Autowired
	private SiteMapper siteMapper;
	
	@Override
	public int saveEntity(Site entity) {
		siteMapper.saveEntity(entity);
		return 0;
	}

	@Override
	public int updateEntity(Site entity) {
		siteMapper.updateEntity(entity);
		return 0;
	}

	@Override
	public Site findByPK(Serializable entityId) {
		if(entityId!=null){
			return siteMapper.findByPK(entityId);
		}else{
			return null;
		}
	}

	@Override
	public int deleteByPKArray(Serializable[] ids) {
		siteMapper.deleteByPKArray(ids);
		return 0;
	}

	@Override
	public Page<Site> findPageListByCriteria(Integer pageNo, Integer pageSize,
			Map<String, Object> para) {
		Page<Site> p = new Page<Site>();
		List<Site> list = siteMapper.findPageListByCriteria(para,
				initRowBounds(pageSize, pageNo, p));
		Long count = siteMapper.findCountByCriteria(para);
		p.setList(list);
		p.setTotalCount(count.intValue());
		return p;
	}

	@SuppressWarnings("rawtypes")
	private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Page p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}


}
